﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormLogin : FormTemplet
    {
        public FormLogin()
        {
            InitializeComponent();
        }

        public FormLogin(FormMain frmMain) : this()
        {
            this.frmMain = frmMain;
        }

        public void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtAccount.Text.Trim().Length < 1)
            {
                this.txtAccount.Focus();
            }
            else if (this.txtPassword.Text.Length < 1)
            {
                this.txtPassword.Focus();
            }
            else
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
                string sIPAddress = hostEntry.AddressList[0].ToString() + "(" + hostEntry.HostName + ")";
                DataSet dataSet = null;
                Operators operators = new Operators();
                try
                {
                    dataSet = operators.Lonin(this.txtAccount.Text.Trim(), this.txtPassword.Text, sIPAddress);
                }
                catch (SqlException ex)
                {
                    if (ex.Number == 50002)
                    {
                        MessageBox.Show("登陆失败\r\n\r\n您输入的账号或者密码错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else if (ex.Number == 50004 || ex.Number == 2627)
                    {
                        MessageBox.Show("请注意！此用户已经登陆。\r\n\r\n    有下列几种情况：\r\n        1、有非正常关闭本程序或者非正常关机的情况\r\n        2、有别人窃取了你的帐号\r\n\r\n    请找管理员处理！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                }
                catch (Exception ex2)
                {
                    MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                operators.Dispose();
                if (dataSet != null)
                {
                    if (dataSet.Tables.Count == 2)
                    {
                        if (dataSet.Tables[0].Rows.Count > 0)
                        {
                            LoginInfo.IsLogined = true;
                            LoginInfo.OperatorID = Convert.ToInt32(dataSet.Tables[0].Rows[0][0]);
                            LoginInfo.OperatorName = dataSet.Tables[0].Rows[0][1].ToString();
                            LoginInfo.LoginName = dataSet.Tables[0].Rows[0][2].ToString();
                            LoginInfo.Password = dataSet.Tables[0].Rows[0][3].ToString();
                            LoginInfo.Role.RoleID = Convert.ToInt32(dataSet.Tables[0].Rows[0][4]);
                            LoginInfo.Role.RoleName = dataSet.Tables[0].Rows[0][5].ToString();
                        }
                        if (dataSet.Tables[1].Rows.Count > 0)
                        {
                            for (int i = 0; i < dataSet.Tables[1].Rows.Count; i++)
                            {
                                LoginInfo.Role.AddPowerItem(new PowerItem(Convert.ToInt32(dataSet.Tables[1].Rows[i][0]), Role.GetPowerName(dataSet.Tables[1].Rows[i][1].ToString()), dataSet.Tables[1].Rows[i][2].ToString()));
                            }
                        }
                       // MessageBox.Show("登陆成功！", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.frmMain.LoginOperation();
                        base.Close();
                    }
                    dataSet.Dispose();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                base.Left = Control.MousePosition.X - this.mousePoint.X;
                base.Top = Control.MousePosition.Y - this.mousePoint.Y;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Default;
        }

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.mousePoint.X = e.X;
                this.mousePoint.Y = e.Y;
                this.Cursor = Cursors.Hand;
            }
        }
    }
}
